package com.Connections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.helloworld.JSONParser;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class DownloadBets extends AsyncTask<Integer, Integer, Long>{

	// URL where the php code is located
	private static final String url_root = "http://rsl.lib.fit.edu/android_connect/";
	private static final String url_getBetRange = url_root + "bet_read_range.php";
	private static final String url_getAllBets = url_root + "bet_read_all.php";
	
	// Limit the amount of bets that can be retrieved
	private static final int MAX_BETS = 10;
	
	// Necessary variables.
	private static JSONArray bets = null;
	private static JSONParser jParser = new JSONParser();
	private static ArrayList<HashMap<String, String>> betsList;
		
	// JSON node names.
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_BETS = "bets";
	private static final String TAG_BETID = "betID";
	private static final String TAG_BETTORID = "bettorID";
	private static final String TAG_ACCEPTORID = "acceptorID";
	private static final String TAG_VALUE = "value";
	
	private Context ctx;
	
	// Used specifically to display a Toast message
	public DownloadBets(Context ctx){
        // Now set context
        this.ctx = ctx;
    }
	
	/**
	 * betsList accessor method
	 *
	 * @return a list of all bets.
	 */
	public static ArrayList<HashMap<String, String>> getBets() {
		return betsList;
	}
	
	@Override
	protected Long doInBackground(Integer... range) {
		// Build parameters.
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("minBet", Integer.toString(range[0])));
		params.add(new BasicNameValuePair("maxBet", Integer.toString(range[1])));
		params.add(new BasicNameValuePair("MAX_BETS", Integer.toString(MAX_BETS)));
		// getting JSON string from URL
		JSONObject json = jParser.makeHttpRequest(url_getBetRange, "GET", params);
		try {
			// Check the SUCCESS TAG
			int success = json.getInt(TAG_SUCCESS);
			
			if (success == 1) {
				// bets found
				// Get array of bets
				bets = json.getJSONArray(TAG_BETS);
				
				// loop through all bets
				for(int i = 0; i < bets.length(); i++) {
					JSONObject c = bets.getJSONObject(i);
					
					// Store each JSON item into a var.
					String betID = c.getString(TAG_BETID);
					String bettorID = c.getString(TAG_BETTORID);
					String acceptorID = c.getString(TAG_ACCEPTORID);
					String value = c.getString(TAG_VALUE);
					
					// create a new HashMap
					HashMap<String, String> map = new HashMap<String, String>();
					
					// add each child to the hashmap
					map.put(TAG_BETID, betID);
					map.put(TAG_BETTORID, bettorID);
					map.put(TAG_ACCEPTORID, acceptorID);
					map.put(TAG_VALUE, value);
					
					// add HashList to ArrayList
					if(betsList == null){
						betsList = new ArrayList<HashMap<String, String>>();
					}
					betsList.add(map);
				}
			} else {
				// no bets found
				// bets will be null
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		// TODO Figure out what to return here.
		// Null is suggested, but it could be useful for a value in the future
		return null;
	}
	
	@Override
	protected void onPostExecute(Long result){
		super.onPostExecute(result);
		// TODO Alter the main, UI thread when the database connection is finished.
		Toast.makeText(this.ctx.getApplicationContext(), "Loading bets done!", Toast.LENGTH_LONG).show();
		this.cancel(true);
	}

}
